home *** CD-ROM | disk | FTP | other *** search
/ Amiga Collections: MegaDisc / MegaDisc 43 (1995-03)(MegaDisc Digital Publishing)(AU)(Disk 2 of 2)[m bamcopy].zip / MegaDisc 43 (1995-03)(MegaDisc Digital Publishing)(AU)(Disk 2 of 2)[m bamcopy].adf / Programming / Pascal_Tutes / Tute11.pas < prev    next >
Pascal/Delphi Source File  |  1995-01-27  |  7KB  |  202 lines

  1. {
  2.  
  3.              «»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»
  4.              «»                                                  «» 
  5.              «»                 TUTORIAL ELEVEN                  «» 
  6.              «»                                                  «» 
  7.              «»                        by                        «» 
  8.              «»                                                  «» 
  9.              «»                   Anthony Peck                   «» 
  10.              «»                                                  «» 
  11.              «»                                                  «» 
  12.              «»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«» 
  13.                                                                     
  14.  
  15.  
  16.          43  !¡  43  !¡  43  !¡  43  !¡  43  !¡  43  !¡  43  !¡  43
  17.  
  18.  
  19.                It's just a small procedure, it won't hurt a bit...
  20.  
  21.    Some of the programs in the last set of tutorials were starting to get 
  22.    too big for their own boots!  Take Tute8.pas for instance, which had 
  23.    several variables, and a couple of different components within the 
  24.    program structure.
  25.  
  26.    A Program is more manageable both to code and debug if it is broken up 
  27.    into smaller tasks.  This is called modularity, and the theory is that 
  28.    each component of the program be no more than a page or so in length.
  29.  
  30.    Pascal allows modularity through PROCEDURES, which are like separate 
  31.    little programs within the main program.
  32.  
  33.    Let's make a program that finds the cube of a number entered by the
  34.    user.  For any number x the cube would be x³.  4³ is 4x4x4 = 64!  It
  35.    would be nice just to be able to separate out the parts of the program
  36.    to do different jobs.  For example we could have three procedures which
  37.    do the following tasks...
  38.  
  39.     Get number;
  40.     Find cube;
  41.     Display result;
  42.  
  43.    Isn't this exciting!  On with the program...}
  44.  
  45.     Program MDTute11 (Input,Output);
  46.  
  47.     {    This program finds the cube of a user entered number
  48.  
  49.                 Author : A N Peck
  50.  
  51.                 Date : 23 October 1994
  52.                 
  53.     Procedures used:
  54.     
  55.         Getnumber - prompts user for a number
  56.         
  57.         Cube - multiplies a number by itself three times
  58.         
  59.         Display - displays the result }
  60.  
  61. {        ^
  62.         |
  63.         |__  This information is handy to have at the start of a
  64.              program which is going to use procedures.  It just
  65.              keeps the code tidy, and makes lovely dressing for a
  66.              side salad.  }
  67.  
  68.     Var number,result: longint;
  69.  
  70.     { ---------------------------------------------------------- }
  71.  
  72. {    ^
  73.     |
  74.     |__  I use one of these to separate out the procedures.  You don't
  75.          need them at all, but it makes the code easier to read and
  76.          follow.  It's another one of those damn cosmetic artifacts. }
  77.  
  78.     Procedure Getnumber;
  79.  
  80. {    ^
  81.     |
  82.     |__  Each procedure commences with the word Procedure, followed by the
  83.          name which you have decided best fits the description of what
  84.          the procedure is about.  You could call them Ralph, Graham and
  85.          Mindy but that doesn't tell the average punter too much about
  86.          what is really happening!  }
  87.  
  88.     { Prompts user for a number }
  89.  
  90. {      ^
  91.       |
  92.       |__  It's a good policy to start each procedure with a comment
  93.            describing what the procedure achieves. }
  94.  
  95.     begin
  96.  
  97. {    ^
  98.     |
  99.     |__  The procedure begins with a "begin"!  }
  100.  
  101.     writeln;
  102.  
  103.     write('     Please enter a whole number: ');
  104.  
  105.     readln(number);
  106.  
  107.     end; { Getnumber }
  108.  
  109. {         ^
  110.          |
  111.          |__  It's good form to finish with a comment noting the name
  112.           of the procedure.  You can also insert a small jam tart
  113.           here if you're really brave.  }
  114.     
  115.     { ---------------------------------------------------------- }
  116.  
  117.     Procedure cube;
  118.     
  119.     { Multiplies a number by itself three times }
  120.  
  121.     begin
  122.     
  123.     result := number*number*number;
  124.     
  125. {    ^
  126.     |
  127.     |__  This procedure contains just the maths bit.  }
  128.  
  129.     end; { cube }
  130.     
  131.     { ---------------------------------------------------------- }
  132.  
  133.     Procedure display;
  134.  
  135.     { Displays the results }
  136.     
  137.     begin
  138.  
  139.     writeln;
  140.  
  141.     writeln('     ','The cube of number ',number,' is ',result);
  142.  
  143.     writeln;
  144.  
  145.     end; { display }    
  146.  
  147.     { ---------------------------------------------------------- }
  148.     { ---------------------------------------------------------- }
  149.  
  150.     begin { Main Program }
  151.         
  152.     Getnumber;
  153.     
  154.     Cube;
  155.         
  156.          Display;
  157.  
  158. {    ^
  159.     |
  160.     |__  The whole of the main program boils down to just three lines.
  161.           The hard work has been farmed out to the procedures, which
  162.          makes the main program look rather wonderful.  Is that easy 
  163.          or am I just a silly man in a big hat?  }
  164.  
  165.      end. { Main Program }
  166.  
  167.     { ---------------------------------------------------------- }
  168.  
  169. {    Well!  All that for just $1.99 plus tax.  We didn't really need
  170.     three procedures for such a simple task, but it does make the
  171.     project a bit easier to follow.  Now if we see a bug, we can
  172.     find in which part of the program it might be located a bit faster.
  173.     Piecemeal examination of a large program is much easier than trying
  174.     to visualise the entire source code in one hit.
  175.  
  176.     If you're bored to death tonight, try to break up tute8.pas into
  177.     procedures. Use the pseudocode first, and then onwards to the
  178.     completed task!
  179.  
  180.  
  181.  
  182.                                        
  183.                                                
  184.                                                
  185.                                                   
  186.                                                   
  187.                                                
  188.              «»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»
  189.              «»                                                  «» 
  190.              «»                Non compos mentis                 «» 
  191.              «»                                                  «» 
  192.              «»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«» 
  193.  
  194.  
  195.          43  !¡  43  !¡  43  !¡  43  !¡  43  !¡  43  !¡  43  !¡  43
  196.  
  197.  
  198.                                                                      }
  199.  
  200.  
  201.  
  202.